<?php

namespace app\common\model\banxi\sys;

use app\common\enums\MenuEndpointEnum;
use app\common\model\common\BaseModel;

/**
 * Attachment模型
 * @property string url              文件物理路径
 * @property int quote            上传(引用)次数
 * @property int last_upload_time 最后上传时间
 */
class SysRoleMenu extends BaseModel
{
    protected $name = 'banxi_sys_role_menu';

    protected $autoWriteTimestamp = true;

    protected $append = [
        'menuEndpointCn'
    ];
    protected $type = [
        'id' => 'string',
        'roleId' => 'string',
        'menuId' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s'
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getMenuEndpointCnAttr($value, $data)
    {
        $value = $value ?: ($data['menuEndpoint'] ?? '');
        return (new MenuEndpointEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    public function listMenuIdByRoleId(string|int $roleId)
    {
        return self::where(['roleId' => $roleId])->column('menuId', '', true);
    }

    public function removeByMenuId($removeIdList, $menuEndpoint)
    {
        return $this->where([
            ['menuId', 'in', $removeIdList],
            ['menuEndpoint', '=', $menuEndpoint]
        ])->delete();
    }


    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------


}